package com.na.tools.search;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import com.na.tools.CommonTools;

public class FindText {

	static final String DIR = "C:\\Users\\yang.li38\\Desktop\\renren\\20150713_平台活动\\乱世专题页";
	
	public static void main(String[] args) throws Exception {
		launch();
	}
	
	protected static void launch() throws Exception {
		File dir = new File(DIR);
		List<File> files = CommonTools.searchFiles(dir, new FileFilter() {
			@Override
			public boolean accept(File file) {
				return file.getName().endsWith(".php");
			}
		});
		byte[] text = "_get_data(".getBytes("UTF-8");
		for (File f : files) {
			if (findText(f, text)) {
				System.out.println("found:::" + f.getAbsolutePath());
//				break;
			}
		}
		System.out.println("over");
	}
	
	protected static boolean findText(File file, byte[] text) {
		FileInputStream in = null;
		try {
			in = new FileInputStream(file);
			int off = 0;
			byte[] buf = new byte[1];
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while (in.read(buf) != -1) {
				if (buf[0] == text[off]) {
					if (++off == text.length) {
						return true;
					}
				} else {
					off = 0;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			CommonTools.closeIO(in);
		}
		return false;
	}
	
}
